package com.example.bookappgraduation;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.bookappgraduation.fragments.CategoriesFragment;
import com.example.bookappgraduation.fragments.FavoritesFragment;
import com.example.bookappgraduation.fragments.HomeFragment;
import com.example.bookappgraduation.fragments.SearchFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView; // 重要：使用这个 import

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNavigationView = findViewById(R.id.bottom_navigation);//连接底部视图

        // 设置默认选中的 Fragment
        loadFragment(new HomeFragment()); // 默认加载 HomeFragment

        bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;// 初始化一个空的Fragment引用
                int itemId = item.getItemId();//  获取被点击项的ID
                //  根据ID判断，创建对应的Fragment实例
                if (itemId == R.id.nav_home) {
                    selectedFragment = new HomeFragment();
                } else if (itemId == R.id.nav_categories) {
                    selectedFragment = new CategoriesFragment();
                } else if (itemId == R.id.nav_search) {
                    selectedFragment = new SearchFragment();
                } else if (itemId == R.id.nav_favorites) {
                    selectedFragment = new FavoritesFragment();
                } else if (itemId == R.id.nav_logout) {
                    logoutUser();
                    return true; // 处理完毕，不加载fragment
                }

                if (selectedFragment != null) {
                    loadFragment(selectedFragment);
                }
                return true;
            }
        });
    }

    private void loadFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();//获取用于管理Activity中所有Fragment的管理器。
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    }

    private void logoutUser() {
        SharedPreferences sharedPreferences = getSharedPreferences(LoginActivity.PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(LoginActivity.KEY_IS_LOGGED_IN, false);
        // 可选: 清除用户名和密码，但通常不建议，除非用户明确要求“忘记我”
        // editor.remove(LoginActivity.KEY_USERNAME);
        // editor.remove(LoginActivity.KEY_PASSWORD);
        editor.apply();

        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // 清除活动栈
        startActivity(intent);
        finish();
    }
}